<?php
    class Users extends CI_Controller{
        
        function __construct() {
            parent::__construct();
            $this->load->model('Users_Model');
            $this->load->helper('form');
            $this->load->library('form_validation');
        }
        
        function index(){
            
        }
        
        function login(){
            $data['title'] = "Acceso a Track Star";
            $data['main_content']='users/login';
            $this->load->view('includes/template_login', $data);
        }
        
        function register(){
            $data['title']= "Crear nueva cuenta";
            $data['main_content']="users/register";
            $this->load->view('includes/template', $data);
        }
        
        function retry_password(){
            
        }
        
        function verify_login(){
            
        } 
        
        function create_account(){
            $this->form_validation->set_rules('name', 'Nombre','trim|required');
            $this->form_validation->set_rules('username', 'Nombre de Usuario','trim|required|callback__username_check');
            $this->form_validation->set_rules('email', 'Email','trim|required|valid_email|callback__email_check');
            $this->form_validation->set_rules('password', 'Contraseña','trim|required|md5');
            $this->form_validation->set_rules('re_password', 'Confirmar Contraseña','trim|required|md5|matches[password]');
            
            $this->form_validation->set_message('required','El Campo %s es requerido');
            $this->form_validation->set_message('valid_email','El %s no es válido');
            $this->form_validation->set_message('_username_check','El %s ya existe');
            $this->form_validation->set_message('_email_check','El %s ya existe');
            $this->form_validation->set_message('matches', 'Las contraseñas no coinciden');
            
            if($this->form_validation->run() == FALSE){
                $this->register();
            }
            else {
                
            }
        }
        
        function _username_check($username){
            return $this->Users_Model->check_username($username);
        }
        
        function _email_check($email){
            return $this->Users_Model->check_email($email);
        }
    }
?>
